<?php

namespace YmPhp\Admin\Http\Controllers\Business;

use YmPhp\Admin\Http\Repositories\Business\BusinessBrowsingHistory;
use YmPhp\Admin\Models\Business\BusinessList;
use YmPhp\Admin\Models\Member\MemberList;
use YmPhp\Admin\Form;
use YmPhp\Admin\Grid;
use YmPhp\Admin\Show;
use YmPhp\Admin\Http\Controllers\AdminController;

class BusinessBrowsingHistoryController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BusinessBrowsingHistory(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('uid');
            $grid->column('','商家信息')->display(function () {
                $business = (new BusinessList())->getDetails($this->uid);
                if (!empty($business)){
                    $businessInfo = $business->name;
                    $businessInfo .= mb_substr ($business->jieshao, 0, 30, "UTF-8");
                }else {
                    $businessInfo = '商家不存在';
                }
                return $businessInfo;

            });
            $grid->column('visiter');
            $grid->column('visiter_name')->display(function () {
                $member= (new MemberList())->getDetails($this->visiter);
                if (!empty($member)){
                    $memberInfo = $member->username."<br>";
                    $memberInfo .=$member->mobile;
                }else {
                    $memberInfo = '用户不存在';
                }
                return $memberInfo;
            });
            $grid->column('note');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BusinessBrowsingHistory(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('visiter');
            $show->field('visiter_name');
            $show->field('note');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BusinessBrowsingHistory(), function (Form $form) {
            $form->display('id');
            $form->text('uid');
            $form->text('visiter');
            $form->text('visiter_name');
            $form->text('note');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
